package com.glc.gao.gavin.rxjavademo.operator;

import android.app.Activity;
import android.view.View;

import com.glc.gao.gavin.rxjavademo.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class DebounceActivity extends BaseActivity {
    @Override
    public int getDescribeText() {
        return R.string.describe_debounce;
    }

    @Override
    protected int getContextView() {
        return R.layout.activity_debounce;
    }

    public void executeDebounce(View view) {

        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                print("发送参数：" + 1);
                emitter.onNext(1); // skip
                print(" 等待" + 400 + "s后发送参数：" + 2);
                Thread.sleep(400);
                emitter.onNext(2); // deliver
                print(" 等待" + 305 + "s后发送参数：" + 3);
                Thread.sleep(305);
                emitter.onNext(3); // skip
                print(" 等待" + 100 + "s后发送参数：" + 4);
                Thread.sleep(100);
                emitter.onNext(4); // deliver
                print(" 等待" + 605 + "s后发送参数：" + 5);
                Thread.sleep(605);
                emitter.onNext(5); // deliver
                emitter.onComplete();
            }
        }).debounce(500, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        print("接受参数：" + integer);
                    }
                });

    }
}
